<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\helper\Time;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class BathRecord extends Backend
{
    
    /**
     * TracePosition模型对象
     * @var \app\admin\model\TracePosition
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\BathRecord;

    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    public function index()
    {
        
        $filter = json_decode(input('filter'),true);

        $where_ = [];
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();   
            if(isset($filter['username']) && $filter['username']){
                $where_['nickname'] = ['like','%'.$filter['username'].'%'];
            }
            if(isset($filter['sid']) && $filter['sid']){
                $where_['sid'] = ['like','%'.$filter['sid'].'%'];
            }

            $total = $this->model
                ->where($where_)
                ->order('id','desc')
                ->count();

            $list = $this->model
                ->where($where_)
                ->order('id','desc')
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function status($ids = null)
    {
        $row = Db::name('bath_record') -> where(['id' => $ids]) -> find();
        $list = \app\admin\model\BathRecord::where(['nickname' => $row['nickname'], 'sid' => $row['sid'],'into_states'=>1,'leave_states'=>0])->select();
        if(!empty($list)&&is_array($list))
        {
            foreach ($list as $key => $value) {
                //处理锁定状态
                $params['locked']=0;
                $params['leave_states']=1;
                Db::name('bath_record') -> where(['id' => $value['id']]) -> update($params);
            }
        }
        $this -> success('已审核');
    }
}
